//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Text;
using System.Windows;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Globalization;

namespace OpenLS.Spreadsheet.UI
{
    sealed class WindowInfo : IXmlSerializable
    {
        public static void InitializeWindow (Window w)
        {
            try
            { 
                if (!File.Exists(fileName))
                    return;
                var i = new WindowInfo();
                using (XmlReader reader = new XmlTextReader(fileName))
                {
                    i.ReadXml(reader);
                    if (i.IsMaximized)
                    {
                        w.WindowState = WindowState.Maximized;
                        return;
                    }
                    i.Left = Math.Max(i.Left, 0);
                    i.Top = Math.Max(i.Top, 0);
                    double max = i.Left + i.Width;
                    if (max > SystemParameters.FullPrimaryScreenWidth)
                    {
                        i.Left = i.Left - (max - SystemParameters.FullPrimaryScreenWidth);
                        i.Left = Math.Max(i.Left, 0);
                    }
                    max = i.Top + i.Height;
                    if (max > SystemParameters.FullPrimaryScreenHeight)
                    {
                        i.Top = i.Top - (max - SystemParameters.FullPrimaryScreenHeight);
                        i.Top = Math.Max(i.Top, 0);
                    }
                    if (!i.isValid)
                        return;
                    w.Width = i.Width;
                    w.Height = i.Height;
                    w.Left = Math.Max(i.Left, 0);
                    w.Top = Math.Max(i.Top, 0);
                }
            }
            catch { }
        }
        public static void StoreWindow(Window w)
        {
            try
            {
                var i = new WindowInfo();
                using (XmlWriter writer = new XmlTextWriter(fileName,Encoding.Default))
                {
                    i.IsMaximized = w.WindowState == WindowState.Maximized;
                    i.Width = w.Width;
                    i.Height = w.Height;
                    i.Left = w.Left;
                    i.Top = w.Top;
                    i.WriteXml(writer);
                }

            }
            catch { }
        }
        static string fileName
        {
            get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "window.xml"); }
        }

        public bool IsMaximized { get; set; }

        public double Left { get; set; }

        public double Top { get; set; }

        private double _width = 500;
        public double Width
        {
            get { return _width; }
            set
            {
                _width = value;
                if (double.IsNaN(_width))
                    _width = 500;
            }
        }

        private double _height = 500;
        public double Height
        {
            get { return _height; }
            set
            {
                _height = value;
                if (double.IsNaN(_height))
                    _height = 500;
            }
        }

        private bool isValid
        {
            get { return Enumerable.All(new[] {Left, Top, Width, Height}, dd => !double.IsNaN(dd)); }
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            //\\reader.ReadStartElement("WindowInfo", "foo");
            //\\reader.ReadStartElement();
            reader.Read();
            //reader.Read();
            //\\ reader.ReadStartElement("WindowInfo");
            IsMaximized = bool.Parse(reader["IsMaximized"]);
            Left = doubleParse(reader,"Left");
            Top = doubleParse(reader,"Top");
            Width = doubleParse(reader,"Width");
            Height = doubleParse(reader,"Height");
            //\\reader.ReadEndElement();
        }
        static double doubleParse (XmlReader reader, string name)
        {
            string s = reader[name];
            if (s == "NaN")
                return double.NaN;
            return double.Parse(s, CultureInfo.InvariantCulture);
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("WindowInfo", "foo");
            writer.WriteStartAttribute("IsMaximized");
            writer.WriteString(IsMaximized.ToString());
            writer.WriteEndAttribute();
            writer.WriteStartAttribute("Left");
            writer.WriteString(Left.ToString(CultureInfo.InvariantCulture));
            writer.WriteEndAttribute();
            writer.WriteStartAttribute("Top");
            writer.WriteString(Top.ToString(CultureInfo.InvariantCulture));
            writer.WriteEndAttribute();
            writer.WriteStartAttribute("Width");
            writer.WriteString(Width.ToString(CultureInfo.InvariantCulture));
            writer.WriteEndAttribute();
            writer.WriteStartAttribute("Height");
            writer.WriteString(Height.ToString(CultureInfo.InvariantCulture));
            writer.WriteEndAttribute();
            writer.WriteEndElement();
        }

        #endregion
    }
}
